<?php

namespace App\Kuafu\Activity\Command;

use App\Kuafu\Activity\Enum\ActivityEnum;
use App\Kuafu\Activity\Model\ActivityModel;
use Illuminate\Console\Command;

class UpdateStatus extends Command
{
    protected $signature = 'kuafu:activity:update_status';

    protected $description = '自动更新活动状态';

    public function handle()
    {
        // 自动开始活动
        ActivityModel::query()
            ->where('status', ActivityEnum::STATUS_PREPARE)
            // startAt = 0时，为长期活动
            ->whereBetween('startAt', [1, time()])
            ->update(['status' => ActivityEnum::STATUS_RUNNING, 'updatedAt' => time(), 'updatedByUid' => 0]);
        // 自动结束活动
        ActivityModel::query()
            ->whereIn('status', [ActivityEnum::STATUS_SUSPEND, ActivityEnum::STATUS_RUNNING])
            // startAt = 0时，为长期活动
            ->where('startAt', '>', 0)
            ->where('endAt', '<=', time())
            ->update(['status' => ActivityEnum::STATUS_ENDED, 'updatedAt' => time(), 'updatedByUid' => 0]);
        return 0;
    }
}
